package com.boarsoft.message.store.jdbc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class MsgDetailTM extends MsgBaseTM {
	private static final Logger log = LoggerFactory.getLogger(MsgDetailTM.class);

	public MsgDetailTM(String target) {
		super(target.concat("_d"));
	}

	@Override
	protected void prepareInsert() {
		StringBuilder sb = new StringBuilder("insert into ").append(table);
		sb.append(" (cMessageId, vRecipient, iAddTime, tStatus, vMemo, tTried)");
		sb.append(" values (?, ?, ?, ?, ?, 0)");
		this.insertSql = sb.toString();
	}

	@Override
	protected void prepareGet() {
		StringBuilder sb = new StringBuilder();
		sb.append("select iAddTime, tStatus, tTried from ").append(table);
		sb.append(" where cMessageId = ? and vRecipientId = ?");
		this.getSql = sb.toString();
	}

	@Override
	protected void prepareUpdate() {
		StringBuilder sb = new StringBuilder("update ").append(table);
		sb.append(" set tStatus = ?, vMemo = ?, iLastTime = ?, tTried = ?");
		sb.append(" where cMessageId = ? and vRecipient = ?");
		this.updateSql = sb.toString();
	}

	// 删除过期数据的sql
	private String prepareDelExpiredData(short... statuses) {
		StringBuilder sb = new StringBuilder();
		sb.append(" DELETE FROM ").append(table).append(" WHERE iAddTime<=? and tStatus in ( ");
		for (int i = 0; i < statuses.length; i++) {
			if (i != (statuses.length - 1)) {
				sb.append("?,");
			} else {
				sb.append("?)");
			}
		}
		String sql = sb.toString();
		log.info("删除detail表过期数据sql:" + sql);
		return sql;
	}

	public void insert(Connection conn, String msgId, String recipient, short status, String memo)
			throws SQLException, IOException {
		PreparedStatement ps = null;
		try {
			// cMessageId, vRecipient, iAddTime, tStatus, vMemo, tTried
			ps = conn.prepareStatement(insertSql);
			ps.setString(1, msgId); // cMessageId
			ps.setString(2, recipient); // vRecipient
			ps.setLong(3, new Date().getTime()); // iAddTime
			ps.setShort(4, status); // tStatus
			ps.setString(5, memo); // vMemo
			ps.executeUpdate();
		} finally {
			JdbcUtil.close(ps);
		}
	}

	public long update(Connection conn, String messageId, String recipient, int tried, short status, String memo)
			throws SQLException {
		long lastTime = new Date().getTime();
		PreparedStatement ps = null;
		try {
			// set tStatus = ?, vMemo = ?, iLastTime = ?, tTried = ?"
			// where cMessageId = ? and vRecipient = ?
			ps = conn.prepareStatement(updateSql);
			ps.setShort(1, status);
			ps.setString(2, memo);
			ps.setLong(3, lastTime);
			ps.setInt(4, tried);
			ps.setString(5, messageId);
			ps.setString(6, recipient);
			ps.executeUpdate();
		} finally {
			JdbcUtil.close(ps);
		}
		return lastTime;
	}

	/**
	 * 删除过期数据
	 * 
	 * @param conn
	 * @param beforeTime
	 * @return
	 * @throws SQLException
	 */
	public int deleteData(Connection conn, long beforeTime, short... statuses) throws SQLException {
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(prepareDelExpiredData(statuses));
			ps.setLong(1, beforeTime);
			for (int i = 0; i < statuses.length; i++) {
				ps.setShort(i + 2, statuses[i]);
			}
			return ps.executeUpdate();
		} finally {
			JdbcUtil.close(ps);
		}
	}

}
